﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlUser : System.Web.UI.UserControl
{
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    ClassExecute.ClassExecute dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }

            //nằm trong thread
            ThreadStart ths1 = new ThreadStart(GetBrandList);
            ThreadStart ths2 = new ThreadStart(GetUserType);
            Thread th1 = new Thread(ths1);
            Thread th2 = new Thread(ths2);
            th1.Start();
            th2.Start();

            th1.Join();
            th2.Join();
            //nằm trong thread
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridSearch();
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtUserName.Text = "";
        txtName.Text = "";
        txtPass.Text = "";
        txtConfirmPass.Text = "";
        ViewState["flag"] = true;
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = 0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                i=dt.ExecuteSQl_int(str,"k_insertUserSystem",CommandType.StoredProcedure,ref error,
                    new SqlParameter("@UserName",txtUserName.Text),
                    new SqlParameter("@UserDescription",txtName.Text),
                    new SqlParameter("@Password",dt.GetMd5Sum(txtPass.Text)),
                    new SqlParameter("@Sitecode",ddlChiNhanh.SelectedValue.ToString()),
                    new SqlParameter("@UserType",ddlThuocNhom.SelectedValue.ToString()),
                    new SqlParameter("@Active",chbCanLogin.Checked));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Thêm mới người dùng hệ thống thành công", uscMsgBox.enmMessageType.Success);
                    txtUserName.Text = "";
                    txtName.Text = "";
                    txtPass.Text = "";
                    txtConfirmPass.Text = "";
                    ViewState["flag"] = false;
                }
                else if (1 == 2)
                {
                    uscMsgBox1.AddMessage("Tên đăng nhập này đã tồn tại", uscMsgBox.enmMessageType.Success);
                    txtUserName.Focus();
                }
            }
            else
            {
                i = dt.ExecuteSQl_int(str, "k_updateUserSystem", CommandType.StoredProcedure, ref error,
                   new SqlParameter("@UserName", txtUserName.Text),
                   new SqlParameter("@UserDescription", txtName.Text),
                   new SqlParameter("@Password", dt.GetMd5Sum(txtPass.Text)),
                   new SqlParameter("@Sitecode", ddlChiNhanh.SelectedValue.ToString()),
                   new SqlParameter("@UserType", ddlThuocNhom.SelectedValue.ToString()),
                   new SqlParameter("@Active", chbCanLogin.Checked));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Cập nhật thành công", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Cập nhật thất bại", uscMsgBox.enmMessageType.Success);
                }
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    void GetBrandList()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getBrandList", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlChiNhanh, "Sitecode", "sitename");

    }
    void GetUserType()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getUserTypeList", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlThuocNhom, "TypeID", "TypeName");
    }
    void BindGridSearch()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_FindUserSystem]", CommandType.StoredProcedure,
            new SqlParameter("@parameter", txtSearch.Text));

    }
    protected void grvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvUser.PageIndex = e.NewPageIndex;
        BindGridSearch();

    }
    protected void grvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
          dt = new ClassExecute.ClassExecute();
          try
          {
              if (e.CommandName == "chon")
              {

                  GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                  Label lblUser = (Label)grv.FindControl("lblUser");
                  DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetUser]", CommandType.StoredProcedure,
                      new SqlParameter("@UserName", lblUser.Text));

                  txtName.Text = db.Rows[0]["UserDescription"].ToString();
                  txtUserName.Text = db.Rows[0]["UserName"].ToString();
                  chbCanLogin.Checked = bool.Parse(db.Rows[0]["Active"].ToString());
                  try
                  {
                      ddlChiNhanh.SelectedValue = db.Rows[0]["Sitecode"].ToString();
                  }
                  catch
                  {
                  }
                  try
                  {
                      ddlThuocNhom.SelectedValue = db.Rows[0]["UserType"].ToString();
                  }
                  catch
                  {
                  }
              }
          }
          catch (Exception ex)
          {

              string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
              dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
          }    
    }
}